/* Copyright(c) 1998-2012, Arnaud Colin
 * All rights reserved.
 *
 * Licence GNU GPL version 3
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   -> Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   -> Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 */


#include "stdafx.h"
#include "Registry.h"



#define MAX_KEY_LENGTH 255
#define MAX_VALUE_NAME 16383



	void CRegistryKey::SetValue(LPWSTR key, BYTE Data[] , DWORD len )
	{	
		DWORD dwRet;

		dwRet = RegSetValueEx( hKey,
                             (LPWSTR)key,
                             NULL,
                             REG_BINARY,
                             Data,
                             len );

		if( dwRet == ERROR_SUCCESS )
		{
			return;
		}

	}


	void CRegistryKey::SetValue(LPWSTR key, LPWSTR Data )
	{

		DWORD dwRet;

		dwRet = RegSetValueEx( hKey,
                             (LPWSTR)key,
                             NULL,
                             REG_SZ,
                             (LPBYTE) Data,
                             (lstrlen(Data) + 1) * sizeof(TCHAR) );

		if( dwRet == ERROR_SUCCESS )
		{
			return;
		}

	}

	void CRegistryKey::SetValue(LPWSTR key, DWORD Data )
	{
		
		
		DWORD cbData;
		DWORD dwRet;

		cbData = sizeof(Data);


		dwRet = RegSetValueEx( hKey,
                             (LPWSTR)key,
                             NULL,
                             REG_DWORD,
                             (BYTE *)&Data,
                             cbData );
		if( dwRet == ERROR_SUCCESS )
		{
			return;
		}

	}

	void CRegistryKey::GetValue(LPWSTR key)
	{

		
		DWORD cbData;
		DWORD dwRet;

		unsigned char rgValue [1024];
		cbData = 1023;


		dwRet = RegQueryValueEx( hKey,
                             (LPWSTR)key,
                             NULL,
                             NULL,
                             (LPBYTE) rgValue,
                             &cbData );

		if( dwRet == ERROR_SUCCESS )
		{
			return;
		}


	}

	CRegistry::CRegistry(HKEY hKey)
	{
		_hKey = hKey;
	}

	 CRegistry* CRegistry::CurrentUser(void)
	{
		return new CRegistry(HKEY_CURRENT_USER);
	}

	 CRegistry* CRegistry::LocalMachine(void)
	{
		return new CRegistry(HKEY_LOCAL_MACHINE);
	}



	CRegistryKey::CRegistryKey(void)
	{
		bWrite = false;
	}

	void CRegistry::CheckSubKey(LPWSTR key)
	{
		CRegistryKey* rk = CRegistry::OpenSubKey(key, true);
            if(rk == NULL)
                rk =  CRegistry::CreateSubKey(key);
			if(rk != NULL)
				delete rk;
	}

	CRegistryKey*  CRegistry::GetSubKey(LPWSTR key)
	{
		CRegistryKey* rk = CRegistry::OpenSubKey(key, true);
            if(rk == NULL)
                rk =  CRegistry::CreateSubKey(key);
           return rk;

	}


	CRegistryKey*  CRegistry::CreateSubKey(LPWSTR key)
	{
		DWORD  Disposition;   
		CRegistryKey* rk = new CRegistryKey();
		rk->bWrite = true;
		if (RegCreateKeyEx( _hKey,
        (LPWSTR)key,
        0,
		NULL,
		REG_OPTION_CREATE_LINK,
		KEY_ALL_ACCESS,
		NULL,
		&rk->hKey, &Disposition) == ERROR_SUCCESS)
		{
				return rk;
		}
		delete rk;
		return NULL;
	}

	CRegistryKey*  CRegistry::OpenSubKey(LPWSTR key, bool bWrite)
	{
		CRegistryKey* rk = new CRegistryKey();
		rk->bWrite = bWrite;
		if (RegOpenKeyEx( _hKey,
        (LPWSTR)key,
        0,
        KEY_READ,
		&rk->hKey) == ERROR_SUCCESS)
		{
				return rk;
		}
		delete rk;
		return NULL;
	}

